<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class ResultsApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$questionsList = $questionsTable->getQuestionslist()->toarray();
		if(count($questionsList)!=0){
			return new JsonModel(array(
				'data' 	=> json_encode($questionsList),
				'value'	=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	1,
			));
		}	
    }
    public function get($course_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$explodeId=explode('-',$course_id);
			$courseId=$explodeId[0];
			$value=$explodeId[1];
			$user_id=$explodeId[2];
			$level=$explodeId[3];
			$subject_id=$explodeId[4];
			$prequestion_id=$explodeId[5];
			
			$getQuestions = $questionsTable->getSingQuestion($courseId,$value,$user_id,$level,$subject_id,$prequestion_id)->toarray();
		if(count($getQuestions)!=0){
			return new JsonModel(array(
				'getdata' 	=> json_encode($getQuestions),
				'value'	=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	1,
			));
		}	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$resultsTable=$this->getServiceLocator()->get('Models\Model\ResultsFactory');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$getQuestion=$resultsTable->getQuestionData($data)->toArray();
		if(count($getQuestion)==0){
			if($data['type']==0){
				$getresults=$resultsTable->getResultData($data)->toArray();
				if(count($getresults)!=0){
					$saveResults=$resultsTable->updateResults($data);
				}else{
					$saveResults = $resultsTable->saveResults($data);
				}
				return new JsonModel(array(
					'output' 	=> 	'success',
					'ResultsId'	=>	$saveResults,
					'value' 	=> 	1,
				));
			}else{
				$updateResults = $resultsTable->updateResults($data);
				return new JsonModel(array(
					'output' 			=> 	'success',
					'updateResultsId'	=>	$updateResults,
					'value' 			=> 	1,
				));
			}
		}else{
			$updateResults = $resultsTable->updateResults($data);
			return new JsonModel(array(
				'output' 			=> 	'fail',
				'value' 			=> 	 0,
			));
		}
    }
    public function update($qid, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$resultsTable=$this->getServiceLocator()->get('Models\Model\ResultsFactory');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$checkQuestion=$resultsTable->checkQuestion($qid)->toArray();
		
		if(count($checkQuestion)==0){
			$saveQuestion = $resultsTable->saveQuestion($qid,$data);
		}else{
			$updateQuestion=$resultsTable->updateQuestion($qid,$data);
		}
		$checktotalResults= $resultsTable->getTotalResults($qid,$data)->toArray();
		$countResults= count($checktotalResults);
		$updateResultsStatus = $resultsTable->updateResultsStatus($data);
		return new JsonModel(array(
			'output' 			=> 	'success',
			'checktotalResults'	=>	$countResults,
			'value' 			=> 	1,
		));
    }
    public function delete($id)
    {
       
    }
}